/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  MainFragment.java   
 * @Package com.jiyi.activity.main   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-21 下午4:38:48   
 * @version V1.0     
 */  
package com.jiyi.activity.main;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.shizhefei.view.indicator.FragmentListPageAdapter;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.IndicatorViewPager.IndicatorFragmentPagerAdapter;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.swift.base.ApplicationBase;
import com.swift.bean.BannerBean;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.jiyi.widget.SlideShowView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainFragment extends Fragment{

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	
	@BindView(R.id.home_lunbo)
	SlideShowView home_lunbo;
	
	@BindView(R.id.id_stickynavlayout_indicator)
	ScrollIndicatorView indicator;
	@BindView(R.id.id_stickynavlayout_viewpager)
	ViewPager mViewPage;
	
	IndicatorViewPager indicatorViewPager;
	
	private LayoutInflater inflate;
	private String[] names = { "吉友秀", "话题", "涨姿势", "资讯" };
	private int size = 4;
	
	List<BannerBean> mBannerList = new ArrayList<BannerBean>();
	
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main, null, false);
		ButterKnife.bind(this,view);
		
		setTop();
		getLunBoData();
		inflate = LayoutInflater.from(getActivity());
		indicator.setScrollBar(new ColorBar(getActivity(), Color.parseColor("#16B0F4"),5));

		// 设置滚动监听
		int selectColorId = R.color.tab_top_on;
		int unSelectColorId = R.color.tab_top;
		int selectSizeId = R.dimen.tab_text_select;
		int unSelectSizeId = R.dimen.tab_text_noselect;
		
		indicator.setOnTransitionListener(new OnTransitionTextListener().setValueFromRes(getActivity(), selectColorId, unSelectColorId, selectSizeId, unSelectSizeId));

		indicatorViewPager = new IndicatorViewPager(indicator, mViewPage);
		indicatorViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
		indicatorViewPager.setPageOffscreenLimit(1);
		return view;
	}
	
	private void setTop() {
		tvBack.setVisibility(View.INVISIBLE);
		tvTitle.setText("吉消宝");
		tvSearch.setVisibility(View.INVISIBLE);
	}
	
	private void setLunBo(List<BannerBean> bannerList) {
		home_lunbo.setBannerList(bannerList);
	}
	
	private void getLunBoData()
	{
		//这里的账号密码参数是用于后面的引用路径，不能删除
		String account = ApplicationBase.LocalData.getString("input.user", "");
		String password = ApplicationBase.LocalData.getString("password", "");

		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("account", account);
		params.put("password", password);

		VolleyRequest.StringRequestPost(getContext(), WebApi.MAIN_BANNER, "",
				params, new StringInterface(getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								JSONObject dataJson = new JSONObject(data);
								String bannerList = dataJson.getString("bannerList");
								
								List<BannerBean> temp = new Gson().fromJson(
										bannerList, new TypeToken<List<BannerBean>>() {
										}.getType());
								if (temp != null) {
									// 获取到轮播的图片
									mBannerList.clear();
									mBannerList.addAll(temp);
									setLunBo(mBannerList);
								}
							} else {
								ToastUtil.getInstances().showMsg("服务器连接异常");
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg("网络连接失败,请检查您的网络");
					}
				});
	}
	
	private class MyAdapter extends IndicatorFragmentPagerAdapter {

		public MyAdapter(FragmentManager fragmentManager) {
			super(fragmentManager);
		}

		@Override
		public int getCount() {
			return size;
		}

		@Override
		public View getViewForTab(int position, View convertView,
				ViewGroup container) {
			if (convertView == null) {
				convertView = inflate.inflate(R.layout.tab_top, container,false);
			}
			TextView textView = (TextView) convertView;
			textView.setText(names[position % names.length]);
			textView.setPadding(20, 0, 20, 0);
			return convertView;
		}

		@Override
		public Fragment getFragmentForPage(int position) {
			Fragment curFragment = null;
			switch (position) {
			case 0:
				// 吉友秀
				curFragment = new MainJYXFragment();
				break;
			case 1:
				// 话题
				curFragment = new MainHuaTiFragment();
				break;
			case 2:
				// 涨姿势
				curFragment = new MainZhiShiFragment();
				break;
			case 3:
				// 资讯
				curFragment = new MainZiXunFragment();
				break;
			default:
				break;
			}

			return curFragment;
		}

		@Override
		public int getItemPosition(Object object) {
			return FragmentListPageAdapter.POSITION_NONE;
		}

	};
	
	
	public void setCurrentItem(int position)
	{
		indicatorViewPager.setCurrentItem(position, false);
	}
}
